package cn.lg.soar.database.handler.type.base;

import cn.lg.soar.common.util.reflect.GenericTypeUtils;
import cn.lg.soar.core.util.InnerSerializeUtils;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;

import java.lang.reflect.Type;
import java.util.List;

/**
 * List<T> 类型处理器
 * @author luguoxiang
 * @date 2022-05-20
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
public abstract class ListTypeHandler<T> extends AbstractJsonTypeHandler<List<T>> {

    private final Class<T> tClass;

    public ListTypeHandler() {
        Type genericType = GenericTypeUtils.getSuperclassGenericType(this.getClass());
        tClass = (Class<T>) genericType;
    }

    public ListTypeHandler(Class<T> type) {
        tClass = type;
    }

    @Override
    protected List<T> parse(String json) {
        return InnerSerializeUtils.parseArray(json, tClass);
    }

    @Override
    protected String toJson(List<T> obj) {
        return InnerSerializeUtils.toJson(obj);
    }

}
